package com.example.invoicing.controller;

import com.example.invoicing.pojo.PageResult;
import com.example.invoicing.pojo.Warehouse;
import com.example.invoicing.service.WarehouseService;
import com.example.invoicing.util.DateUtil;
import com.example.invoicing.util.ReturnUtil;
import com.example.invoicing.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class WarehouseJsonController {
    @Autowired
    private WarehouseService warehouseService;

    //删除一条数据
    @ResponseBody
    @RequestMapping(value = "/member/deleteWarehouse", method = RequestMethod.POST)
    public ModelMap deleteWarehouse(@Validated @RequestBody Warehouse warehouse, BindingResult result) {
        try {

            if(StringUtils.isEmpty(warehouse.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            int count = warehouseService.deleteByPrimaryKey(warehouse.getId());
            if (count == 0){
                return ReturnUtil.error("删除失败");
            }

            return ReturnUtil.success("删除数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //添加一条数据
    @ResponseBody
    @RequestMapping(value = "/member/addWarehouse", method = RequestMethod.POST)
    public ModelMap addWarehouse(@Validated @RequestBody Warehouse warehouse, BindingResult result) {
        try {

            //随机产生一组加密字符
            warehouse.setId(UuidUtil.getUUID());
            //获取到当前时间
            warehouse.setCreateTime(DateUtil.getCurrentTime());
            int count = warehouseService.insertSelective(warehouse);
            if (count == 0){
                return ReturnUtil.success("添加数据失败");
            }

            return ReturnUtil.success("添加数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //查询一条数据
    @ResponseBody
    @RequestMapping(value = "/member/queryWarehouse", method = RequestMethod.POST)
    public ModelMap queryWarehouse(@Validated @RequestBody Warehouse warehouse, BindingResult result) {
        try {

            if(StringUtils.isEmpty(warehouse.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            Warehouse warehouse1 = warehouseService.selectByPrimaryKey(warehouse.getId());
            if (warehouse1 == null){
                return ReturnUtil.error("查询数据失败");
            }

            return ReturnUtil.success("查询数据成功", warehouse1);

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //更新一条数据
    @ResponseBody
    @RequestMapping(value = "/member/updateWarehouse", method = RequestMethod.POST)
    public ModelMap updateWarehouse(@Validated @RequestBody Warehouse warehouse, BindingResult result) {
        try {

            if(StringUtils.isEmpty(warehouse.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            //更新修改时间
            warehouse.setUpdateTime(DateUtil.getCurrentTime());

            int count = warehouseService.updateByPrimaryKeySelective(warehouse);
            if (count == 0){
                return ReturnUtil.error("更新数据失败");
            }

            return ReturnUtil.success("更新数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //集合于分页查询，所有查询，模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allWarehouseList",method = RequestMethod.POST)
    public ModelMap allWarehouseList(@Validated @RequestBody Warehouse warehouse, BindingResult result){
        try {

            List<Warehouse> warehouseList = warehouseService.queryWarehouseList(warehouse);
            if (warehouseList.size() == 0){
                return ReturnUtil.error("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = warehouseService.queryCountLike(warehouse);

            //加载实体将数据返回
            PageResult pageResult = new PageResult();
            if (warehouse.getPageResult() != null){
                pageResult.setCurrentPage(warehouse.getPageResult().getCurrentPage());
                pageResult.setPageSize(warehouse.getPageResult().getPageSize());

            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(warehouseList);

            return ReturnUtil.success("查询数据成功", pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }
}
